package ZooManagement;
import java.util.ArrayList;
import java.util.HashMap;

public class ZooManager {
    public HashMap<String, Zoo> zooMap = new HashMap<>();

    public void addAnimal(String zooName, Animal animal)
    {
        Zoo zoo = zooMap.get(zooName);
        zoo.addAnimal(animal);
    }

    public void deleteAnimal(String zooName, String animalName)
    {
        Zoo zoo = zooMap.get(zooName);
        zoo.deleteAnimal(animalName);
    }

    public boolean moveAnimal(String fromZoo, String toZoo, String animalName)
    {
        Zoo sourceZoo = zooMap.get(fromZoo);
        Zoo targetZoo = zooMap.get(toZoo);
        if (sourceZoo == null) {
            System.out.println("Source Zoo not found: " + fromZoo);
            return false;
        }

        boolean isEqual = fromZoo.equals(toZoo);
        if (isEqual) {
            return false;
        }

        int index = sourceZoo.findAnimal(animalName);
        if (index == -1) {
            System.out.println("Animal " + animalName + " not found in " + fromZoo);
            return false;
        }
        Animal animal = sourceZoo.getAnimal(index);

        Item vehicle = new Item("Truck", 100.0, "V001");
        Item fuel = new Item("Diesel", 50.0, "F001");
        String[] caretakers = {"John", "Jane"};
        Logistics logistics = new Logistics(vehicle, fuel, caretakers);
        sourceZoo.moveAnimal(animalName, targetZoo, logistics);
        return true;
    }

    public ArrayList<Animal> listAnimals(String zooName)
    {
        Zoo zoo = zooMap.get(zooName);
        if (zoo != null) {
            ArrayList<Animal> animals = new ArrayList<>();
            for (int i = 0; i < zoo.getCounter(); i++) {
                animals.add(zoo.getAnimal(i));
            }
            return animals;
        } else {
            System.out.println("Zoo not found: " + zooName);
            return null;
        }
    }

    public double getTransportCost(Zoo zoo)
    {
        Item vehicle = new Item("Truck", 100.0, "V001");
        Item fuel = new Item("Diesel", 50.0, "F001");
        String[] caretakers = {"John", "Jane"};
        Logistics logistics = new Logistics(vehicle, fuel, caretakers);
        return zoo.getLogisticsPrice(logistics);
    }

    public int findAnimal(String zooName, String animalName)
    {
        Zoo zoo = zooMap.get(zooName);
        if (zoo != null) {
            return zoo.findAnimal(animalName);
        } else {
            System.out.println("Zoo not found: " + zooName);
            return -1;
        }
    }
}
